package org.barbershop.krpc;

import javassist.*;
import org.barbershop.krpc.proxy.service.TestInterface;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException, CannotCompileException, NotFoundException, IllegalAccessException, InstantiationException {
        ClassPool pool = ClassPool.getDefault();
        CtClass test = pool.get("org.barbershop.krpc.proxy.service.TestInterface");
//
//        test.writeFile();
//
//
        CtClass testImpl = pool.makeClass("TestImpl");
        testImpl.setInterfaces(new CtClass[]{test});
        CtMethod ctMethod =CtMethod.make("public void test(){ System.out.println(\"Hello.say():\"); } ",testImpl);
        //CtConstructor ctConstructor = new CtConstructor(new CtClass[0],testImpl);
        //testImpl.addConstructor(ctConstructor);
        testImpl.addMethod(ctMethod);
        System.out.println(testImpl.toString());
        Class c = testImpl.toClass();
        TestInterface h = (TestInterface)c.newInstance();
        h.test();

//        ClassPool cp = ClassPool.getDefault();
//        CtClass cc = cp.get("org.barbershop.krpc.Hello");
//        CtMethod m = cc.getDeclaredMethod("say");
//        m.insertBefore("{ System.out.println(\"Hello.say():\"); }");
//        Class c = cc.toClass();
//        Hello h = (Hello)c.newInstance();
//        h.say();
    }
}
